Data Visualization Project 02

revised version of mini-project 02 goes here

By Kyle Dean

library(tidyverse)
## -- Attaching packages -------------------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.3     v dplyr   1.0.2
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## -- Conflicts ----------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(gapminder)
## Warning: package 'gapminder' was built under R version 4.0.5
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(sf)
## Warning: package 'sf' was built under R version 4.0.5
## Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1
Billboard <- read_csv("https://raw.githubusercontent.com/reisanar/datasets/master/all_billboard_summer_hits.csv")
## Parsed with column specification:
## cols(
##   .default = col_double(),
##   key = col_character(),
##   mode = col_character(),
##   track_uri = col_character(),
##   key_mode = col_character(),
##   playlist_name = col_character(),
##   playlist_img = col_character(),
##   track_name = col_character(),
##   artist_name = col_character(),
##   album_name = col_character(),
##   album_img = col_character()
## )
## See spec(...) for full column specifications.
Bill2001 <- filter(Billboard, playlist_name == "summer_hits_2001")
Bill2001Plot <- ggplot(
  data = Bill2001,
  mapping = aes(x = danceability, y = loudness, 
                color = mode)) +
  geom_point(aes(text = key_mode), size = 5) +
  scale_x_log10() +
  theme_minimal()
## Warning: Ignoring unknown aesthetics: text
ggplotly(Bill2001Plot)
interactive_plot <- ggplotly(
  Bill2001Plot, tooltip = "text"
) 
interactive_plot
htmlwidgets::saveWidget(interactive_plot, "fancy_plot.html")
Billboard_cor <- Billboard %>% 
  select(danceability, energy, speechiness, acousticness, instrumentalness) %>% 
  cor()
# only consider upper triangular part of
# correlation matrix
Billboard_cor[lower.tri(Billboard_cor)] <- NA
# format the matrix of correlation
Billboard_cor_long <- Billboard_cor %>% 
  as.data.frame() %>% 
  rownames_to_column("Variable2") %>% 
  pivot_longer(cols = -Variable2,
               names_to = "Variable1",
               values_to = "cor") %>% 
  mutate(nice_cor = round(cor, 2)) %>% 
  mutate(Variable1 = fct_inorder(Variable1),
         Variable2 = fct_inorder(Variable2)) %>% 
  filter(!is.na(cor)) %>% 
  filter(Variable2 != Variable1)
ggplot(Billboard_cor_long, 
       aes(x = Variable2, 
           y = Variable1, 
           fill = cor)) +
  geom_tile() +
  geom_text(aes(label = nice_cor)) +
  scale_fill_gradient2(
    low = "#F63719", 
    mid = "white", 
    high = "#3B9AB2", 
    limits = c(-1, 1)
    ) +
  labs(x = NULL, y = NULL) +
  coord_equal() +
  theme_minimal() +
  theme(panel.grid = element_blank(), legend.position = "none")